﻿// ****************************************************************************** //
//       Module to check if there are the conditions for the final victory        //
//           or the final loss. It handles also the game timer countdown          //
// ****************************************************************************** //

module WinLogic

open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Input
open Microsoft.Xna.Framework.Graphics
open MapGrid
open CarsSimulation
open FrogSimulation
open FrogTypes
open RiverSimulation
open Utilities
open Casanova
open Coroutines
open Math
open SI
open GameState

let mutable time_finished: bool = false
let mutable win_level : bool = false
let mutable lost_level : bool = false
let mutable time_limit : float32 = 40.0f;

// ****************************************************************************** //
//                               Level definitions                                //
// ****************************************************************************** //

let check_level () =
 reset_speed()
 match game_state.level with
 | 0 -> update_car_speed (1.0f)
        time_limit <- 40.0f;
 | 1 -> update_car_speed (1.0f)
        time_limit <- 40.0f;
 | 2 -> update_car_speed (1.2f)
        time_limit <- 30.0f;
 | 3 -> update_car_speed (1.4f)
        update_river_speed(0.8f)
        time_limit <- 20.0f;
 | 4 -> update_car_speed (1.6f)
        update_river_speed_hard(0.7f,1.9f)
        time_limit <- 20.0f;
 | v -> update_car_speed (0.34f * (float32)v)
        update_river_speed_hard(0.14f * (float32)v,0.39f * (float32)v)
        time_limit <- 15.0f;

let update_win_check(dt:float32) =
    if starting_position = true then 
        time_finished <- false
        //win_level <- false

    game_state.time_counter <- game_state.time_counter + dt

    if game_state.time_counter >= time_limit then
        time_finished <- true
        FrogSimulation.reset_state()
        game_state.Life <- game_state.Life - 1
        game_state.time_counter <- 0.0f
        FrogSimulation.reset_state()
    if game_state.Life < 0 then
        time_finished <- false
        lost_level <- true
        game_state.winbox_check <- [|false;false;false;false;false|]
        game_state.count_win <- 0
        FrogSimulation.reset_state()
        game_state.Life <- 3
        game_state.time_counter <- 0.0f
        game_state.level <- 1
        check_level()
    if game_state.count_win = 5 then
        game_state.count_win <- 0 
        game_state.winbox_check <- [|false;false;false;false;false|]
        game_state.level <- game_state.level + 1
        check_level()
        FrogSimulation.reset_state()
        if game_state.Life < 3 then game_state.Life <- game_state.Life + 1
        game_state.time_counter <- 0.0f
        win_level <- true
        lost_level <- false
        FrogSimulation.win_trigger <- 0